﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace UE.SharePoint.ImportExport.BLL.SharePoint
{
    /// <summary>
    /// This is used to look up sharepoint lookup column value
    /// </summary>
    public class LookupFinder
    {
        protected SPList sourceList;
        private SPList lookupList;
        private string internalColumnName; 

        /// <summary>
        /// 
        /// </summary>
        public SPList LookupList
        {
            get
            {
                if (this.lookupList == null)
                {
                    this.InitLookup();
                }
                return this.lookupList;
            }
            protected set
            {
                this.lookupList = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        public LookupFinder(SPList list)
            : this(list, string.Empty)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <param name="internalColumnName"></param>
        public LookupFinder(SPList list, string internalColumnName)
        {
            this.sourceList = list;
            this.internalColumnName = internalColumnName;
        }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void InitLookup()
        {
            SPField fieldByInternalName = this.sourceList.Fields.GetFieldByInternalName(this.internalColumnName);
            if (fieldByInternalName.Type == SPFieldType.Lookup)
            {
                SPFieldLookup sPFieldLookup = fieldByInternalName as SPFieldLookup;
                SPWeb spWeb = this.sourceList.ParentWeb;
                Guid spListGuid = new Guid(sPFieldLookup.LookupList);
                SPList spList = null;
 
                while(spWeb!=null && spList== null)
                {
                    try
                    {
                        spList = spWeb.Lists[spListGuid];
                    }
                    catch
                    {
                        spList = null;
                    }

                    spWeb = spWeb.ParentWeb;
                }

                this.lookupList = spList;
                                
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lookupValue"></param>
        /// <returns></returns>
        public virtual List<SPListItem> Find(IList<LookupItem> lookupItems)
        {
            SPQuery query = this.MakeQuery(lookupItems);
            query.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='Title' />"; 
            var items = this.LookupList.GetItems(query).Cast<SPListItem>().ToList();
            return items;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="lookupValue"></param>
        /// <returns></returns>
        protected SPQuery MakeQuery(IList<LookupItem> lookupItems)
        {
            bool isHasMulti = lookupItems.Count > 1;
            int index = 0;
            SPQuery spQuery = new SPQuery();
            StringBuilder strQuery = new StringBuilder();
            strQuery.Append("<Where>");

            if (isHasMulti) strQuery.Append("<Or>"); 

            foreach (var item in lookupItems)
            {
                if (index == 2) break;
                strQuery.AppendFormat("<Eq><FieldRef Name='{0}' /><Value Type='Text'>{1}</Value></Eq>", item.FieldInternalName, item.Value);
                index++;
            }

            if (isHasMulti) strQuery.Append("</Or>"); 

            strQuery.Append("</Where>");

            spQuery.Query = strQuery.ToString();
            spQuery.ViewFieldsOnly = true;

            return spQuery;
        }
    }
}
